// 默认配置
const express = require('express');
const router = express.Router();
const cors = require("cors");
const nodemailer = require("nodemailer");
const { Concat } = require('./src/Components/Concat');

// server 使用去 发送邮件
const app = express();
app.use(cors());
app.use(express.json());
app.use("/", router);
app.listen(5000, () => console.log("Server Running"));
console.log(process.env.EMAIL_USER);
console.log(process.env.EMAIL_PASS);

// 配置三方邮箱
const contactEmail = nodemailer.createTransport({
    service: 'qq',
    auth: {
        user: "********@qq.com",
        pass: ""
    },
});
// 邮箱授权
contactEmail.verify((error) => {
    if (error) {
        console.log(error);
    } else {
        console.log("Ready to Send");
    }
})

// 发送请求
router.post("/contact", (req, res) => {
    const name = req.body.firstName + req.body.lastName;
    const email = req.body.email;
    const message = req.body.message;
    const phone = req.body.phone;
    const mail = {
        from: name,
        to: "********@qq.com",
        subject: "Contact Form Submission - Portfolio",
        html:`<p>Name: ${name}</p>
        <p>Email: ${email}</p>
        <p>Phone: ${phone}</p>
        <p>Message: ${message}</p>`
    }
    // 发送邮箱
    contactEmail.sendMail(mail, (error) => {
        if (error) {
          res.json(error);
        } else {
          res.json({ code: 200, status: "Message Sent" });
        }
      });
})
